<?php
/**
 * Created by PhpStorm.
 * User: hui
 * Date: 2017/2/18
 * Time: 22:47
 */
namespace app\admin\controller;

use think\Loader;

class Admin extends Base
{
    protected $post = [];

    public function _initialize()
    {
        parent::_initialize();
        $this->post = input('post.');
    }

    public function index()
    {
        $admin = Loader::model('admin','logic');
        $list = $admin->select();
        $title = '管理员设置';
        $this->assign('title',$title);
        $this->assign('list', $list);
        return $this->fetch();
    }

    public function add()
    {
        if ($this->request->isPost()){
            $res = $this->validate($this->post, 'Admin.add');
            if (true !== $res) {
                $this->error($res);
            }
            $admin = Loader::model('admin','logic');
            $res = $admin->add();
            if (!empty($res)) {
                $this->success('新增成功');
            } else {
                $this->error('新增失败');
            }
        }
        return $this->fetch();
    }

    public function save()
    {
        if ($this->post) {
            $res = $this->validate($this->post, 'Admin.save');
            if (true !== $res) {
                $this->error($res);
            }
            $admin = Loader::model('admin','logic');
            $res = $admin->allowField(true)->save($this->post);
            if (!empty($res)) {
                $this->success('新增成功');
            } else {
                $this->error('新增失败');
            }
        }
    }

    public function edit()
    {
        if ($this->post) {
            if ($this->post['action'] == 'edit'){
                $res = $this->validate($this->post, 'Admin.edit');
                if (true !== $res) {
                    $this->error($res);
                }
                $this->success('操作成功');
            }elseif ($this->post['action'] == 'delete'){
                $this->success('删除成功');
            }
        }
    }
}